package com.czy.owner.net.api;

import com.czy.owner.global.Constants;
import com.czy.owner.net.exception.HttpTimeException;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.utils.MyLog;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.lang.ref.SoftReference;

import retrofit2.Retrofit;
import rx.Observable;
import rx.functions.Func1;

/**
 * 请求数据统一封装类
 * Created by wangyang on 2017/4/26.
 */
public abstract class BaseApi<T> implements Func1<BaseResultEntity<T>, T> {
    //rx生命周期管理
    private SoftReference<RxAppCompatActivity> rxAppCompatActivity;
    /*回调*/
    private  HttpOnNextListener  listener;
    /*是否能取消加载框   是否取消订阅*/
    private boolean cancel;
    /*是否显示加载框*/
    private boolean showProgress;
    private String progressTitle = "";
    /*是否需要缓存处理*/
    private boolean cache;
    /*基础url*/
    private  String baseUrl= Constants.APP_API_BASE;
    /*方法-如果需要缓存必须设置这个参数；不需要不用設置*/
    private String mothed;
    /*超时时间-默认15秒*/
    private int connectionTime = 15;
    /*有网情况下的本地缓存时间默认60秒*/
    private int cookieNetWorkTime = 60;
    /*无网络的情况下本地缓存时间默认30天*/
    private int cookieNoNetWorkTime = 24*60*60*30;


    private boolean disabledRetry = false;



    private String mCachePage = "1";

    public BaseApi(HttpOnNextListener listener, RxAppCompatActivity rxAppCompatActivity) {
        setListener(listener);
        setRxAppCompatActivity(rxAppCompatActivity);
        setShowProgress(false);
        setCache(false);
    }

    /**
     * 设置参数
     *
     * @param retrofit
     * @return
     */
    public abstract Observable getObservable(Retrofit retrofit);


    public boolean isDisabledRetry() {
        return disabledRetry;
    }

    public void setDisabledRetry(boolean disabledRetry) {
        this.disabledRetry = disabledRetry;
    }

    public int getCookieNoNetWorkTime() {
        return cookieNoNetWorkTime;
    }

    public void setCookieNoNetWorkTime(int cookieNoNetWorkTime) {
        this.cookieNoNetWorkTime = cookieNoNetWorkTime;
    }

    public int getCookieNetWorkTime() {
        return cookieNetWorkTime;
    }

    public void setCookieNetWorkTime(int cookieNetWorkTime) {
        this.cookieNetWorkTime = cookieNetWorkTime;
    }

    public String getMothed() {
        return mothed;
    }

    public int getConnectionTime() {
        return connectionTime;
    }

    public void setConnectionTime(int connectionTime) {
        this.connectionTime = connectionTime;
    }

    public void setMothed(String mothed) {
        this.mothed = mothed;
    }

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public String getUrl() {
        return baseUrl+mothed;
    }

    public void setRxAppCompatActivity(RxAppCompatActivity rxAppCompatActivity) {
        this.rxAppCompatActivity =new SoftReference(rxAppCompatActivity) ;
    }

    public boolean isCache() {
        return cache;
    }

    public void setCache(boolean cache) {
        this.cache = cache;
    }

    public boolean isShowProgress() {
        return showProgress;
    }

    public void setShowProgress(boolean showProgress) {
        this.showProgress = showProgress;
    }
    public void setShowProgress(boolean showProgress, String progressTitle) {
        this.showProgress = showProgress;
        this.progressTitle = progressTitle;
    }

    public String getProgressTitle() {
        return progressTitle;
    }

    public void setProgressTitle(String progressTitle) {
        this.progressTitle = progressTitle;
    }

    public boolean isCancel() {
         return cancel;
     }

     public void setCancel(boolean cancel) {
         this.cancel = cancel;
     }

     public  HttpOnNextListener  getListener() {
             return listener;
     }

     public void setListener(HttpOnNextListener listener) {
         this.listener =  listener ;
     }


    public String getmCachePage() {
        return mCachePage;
    }

    public void setmCachePage(String mCachePage) {
        this.mCachePage = mCachePage;
    }

    /*
                 * 获取当前rx生命周期
                 * @return
                 */
    public RxAppCompatActivity getRxAppCompatActivity() {
        return rxAppCompatActivity.get();
    }

    @Override
    public T call(BaseResultEntity<T> httpResult) {
        if (!httpResult.isFlag()) {
            throw new HttpTimeException(httpResult.getExp());
        }else {
            return httpResult.getData();
        }
    }
}
